package com.lancoo.edu.platform.operation.commons.controller;

import com.lancoo.edu.platform.operation.commons.controller.base.BaseResultController;
import com.lancoo.edu.platform.operation.commons.dto.BaseResult;
import com.lancoo.edu.platform.operation.commons.service.api.UserInfoApiService;
import com.lancoo.edu.platform.operation.commons.service.bo.UserInfoBoService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户信息控制器
 * @author pky
 * @date 2021-09-014
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/common/user")
public class UserController extends BaseResultController {

    final UserInfoBoService userInfoBoService;
    final UserInfoApiService userInfoApiService;

    /**
     * 通过token 获取用户信息
     * @param token 令牌
     * @return BaseResult2 通用返回结果
     */
    @GetMapping(value = "")
    public BaseResult getByToken(String token) throws Exception {
        if(StringUtils.isBlank(token)) {
            return error("token不可为空");
        }
        return success(userInfoBoService.getByToken(token));
    }

    /**
     * 退出登录
     * @param token 令牌
     * @param userId 用户ID
     * @return 通用返回结果
     */
    @GetMapping(value = "/logout")
    public BaseResult logout(String token, String userId) {
        if(StringUtils.isBlank(token)) {
            return error("token 不可为空");
        }
        userInfoApiService.logout(token,userId);
        return success();
    }

}
